﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using JsonTest1.Net;
using JsonTest1.Configure;
using JsonTest1.Data;
using System.IO;
using Newtonsoft.Json;
using JsonTest1.Json;

namespace JsonTest1.NetEngine
{
    public class RadioPlayListEngine:HttpMessage
    {
         public RadioPlayListEngine()
        {
            base.init();
        }


        /*
         request by ids
         */
        public void Request(Radio radio, int n, string encoding)
        {
           
            String url ="track_id/track/json/radio_track_inradioplaylist/?radio_id="+radio.Id+"&nshuffle="+n*10+"&n="+n;
            BeginRequest(Constants.GET_API + url);

        }

       

        public override bool HttpGeneratorDataCallback(Stream stream)
        {

            try
            {
                //解析json,过滤错误
                string jsonStr = new StreamReader(stream).ReadToEnd();
                if (jsonStr == null)
                {
                    //AlbumFunctions.getAlbums();
                    HttpFailCalback(1, 1, new ResultData(1, 1, ""));
                }
                else
                {
                    
                    int[] tracks_id=RadioPlayListFunctions.getPlayListIds(jsonStr);
                    //开始第二个请求和第三个请求，用同步的方式



                  /*  if (httpStateListener != null)
                        httpStateListener.HttpDataStreamCalback(stream, new ResultData(0, 0, RadioFunctions.getRadios(jsonStr)));*/

                }
            }
            catch (JsonException e)
            {
                HttpFailCalback(1, 1, new ResultData(1, 1, ""));
            }
            catch (Exception e)
            {

            }

            return true;
        }
    }
    
}
